export * as localstorage from "./localStorage";
import { PublishItem } from "@/api/api";
import moment from "moment";

// 判断类型
class queryType {
  public value;
  constructor(value: any) {
    this.value = value;
  }
  query() {
    return Object.prototype.toString.call(this.value);
  }

  type() {
    const target = this.query();
    switch (target) {
      case "[object Object]":
        return "object";
      case "[object String]":
        return "string";
      case "[object Number]":
        return "number";
      case "[object Boolean]":
        return "boolean";
      case "[object Array]":
        return "array";
      case "[object Undefined]":
        return "undefined";
      case "[object Null]":
        return "null";
      case "[object Promise]":
        return "promise";
    }
  }
}

type QueryTypeByTarget = (
  v: any
) =>
  | "object"
  | "string"
  | "number"
  | "boolean"
  | "array"
  | "undefined"
  | "null"
  | "promise";
export const queryTypeByTarget = (target: any) => {
  const QueryType = new queryType(target);
  const r = QueryType.type();
  return r;
};

// 根据role来判断是司机还是乘客，如果是司机展示司机相关的标签，如果是乘客展示乘客相关的标签
type ShowTagsByRole = (item: PublishItem) => Array<string>;
export const showTagsByRole: ShowTagsByRole = (item) => {
  const { role } = item;
  let res: Array<string> = [];
  const baseGenerationFunciont = (
    someTags: Array<{ key: string; label: string }>,
    obj: any
  ) => {
    someTags.forEach((t: any) => {
      let newTag = "";
      if (t.key === "time" && obj?.[t.key]) {
        const _t = moment(obj?.[t?.key]).format("YYYY-MM-DD HH:mm:ss");
        newTag = `${t.label}: ${_t}`;
      } else {
        newTag = obj?.[t.key] ? `${t.label}: ${obj?.[t.key]}` : "";
      }

      res.push(newTag);
    });
  };
  const driverTags = [
    // {
    //   key: "carNumber",
    //   label: "车牌号",
    // },
    {
      key: "seat",
      label: "空位",
    },

    {
      key: "carModel",
      label: "车型",
    },
    {
      key: "via",
      label: "途经地",
    },
  ];
  const passengerTags = [
    {
      key: "price",
      label: "价格",
    },
    {
      key: "peopleNumber",
      label: "乘坐人数",
    },
    // {
    //   key: "endLocation",
    //   label: "目的地",
    // },
  ];
  const baseTags = [
    {
      key: "time",
      label: "出发时间",
    },
    // {
    //   key: "startLocation",
    //   label: "出发地",
    // },
    // {
    //   key: "endLocation",
    //   label: "目的地",
    // },
    // {
    //   key: "created_at",
    //   label: "创建时间",
    // },
    {
      key: "remarks",
      label: "备注",
    },
  ];
  baseGenerationFunciont(baseTags, item);
  if (role === "driver") {
    baseGenerationFunciont(driverTags, item);
  }
  if (role === "passenger") {
    baseGenerationFunciont(passengerTags, item);
  }
  return res.filter((r) => r !== "");
};
